package serverlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.omg.CORBA.Request;

import connectDB.IDB;
import connectDB.PacketDB;
import entities.Packetentities;
import services.Packetservices;
//import until.Dodistributor;
//import until.Dodistributor.Actiondistributor;
import until.Dopacket.Actionpacket;


/**
 * Servlet implementation class Packetcontroller
 * 
 */
@WebServlet("/Packetcontroller")
public class Packetcontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private Packetservices packservices;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Packetcontroller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {


		String action = request.getParameter("btaction");
		
		if(action.equals("delete")){
			this.doDelete(request, response);
		}		
		
		if(action.equals("edit"))   //edit
			{
				this.doEdit(request, response);
			}
		
		// code ben duoi ....
		  else if(action.equals("save"))
			{
				this.doAdd(request, response);
			}
	}

	

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {


		try {
			IDB<Packetentities, Boolean, String> acc = new PacketDB();
			int packetid = 0;
			String packetname = request.getParameter("packname");
			Packetentities land = new Packetentities(packetid, packetname);
			boolean result = acc.add(land);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/Packetlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	

	private void doEdit(HttpServletRequest request, HttpServletResponse response) throws IOException {


		try {
    		IDB<Packetentities, Boolean, String> acc = new PacketDB();
			int id = Integer.parseInt(request.getParameter("packid"));
			String landName = request.getParameter("packname");
			Packetentities land = new Packetentities(id, landName);
			boolean result = acc.update(land);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/Packetlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

	
	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		try {	
		String x = req.getParameter("packetid");
		
		IDB<Packetentities,Boolean,String> acc = new PacketDB();
		
		boolean result = acc.delete(x);
		String url = "/Manager/Packetlist.jsp";
		RequestDispatcher dis = req.getRequestDispatcher(url);
		dis.forward(req, resp);
		
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
